package cn.kgc.web;



import cn.kgc.entity.Student;
import cn.kgc.service.StudentService;

import cn.kgc.service.impl.StudentServiceImpl;
import cn.kgc.util.Pager;
import com.alibaba.fastjson.JSON;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(urlPatterns = "/student")
public class StudentServlet extends BaseServlet{
    //查询
    public String searchs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int currentPage = 1;//当前页
        int currentSize = 2;//每页显示的条数
        //获取页码
        String currentPagestr = request.getParameter("cp");
        //获取每页显示的条数
        String currentSizestr = request.getParameter("ps");
        if(currentPagestr!=null && !"".equals(currentPagestr)){
            currentPage = Integer.parseInt(currentPagestr);
        }
        if(currentSizestr!=null && !"".equals(currentSizestr)){
            currentSize = Integer.parseInt(currentSizestr);
        }
        //获取学号，姓名，专业 模糊查询
        String stunum = request.getParameter("stunum");
        String name = request.getParameter("name");
        String specialty =request.getParameter("specialty");


        StudentService studentService=new StudentServiceImpl();
        List<Student> studentList = studentService.searchs(currentPage,currentSize,stunum,name,specialty);

        //获取总页数
        int totalPage = studentService.totalPage(currentSize,stunum,name,specialty);
        //设置起始页码和结束页码
        int begin = currentPage-2;
        int end = currentPage+2;
        if(currentPage<=2){
            begin = 1;
        }
        if(currentPage>=totalPage-2){
            end = totalPage;
        }
        //获取总的记录数
        int totalCount = studentService.totalCount(stunum,name,specialty);
        //将总的记录数，当前页码，每页显示条数，页码数量放入pager对象中
        Pager pager = new Pager(totalCount,currentPage,currentSize,10);
        //将查询结果集teacherList放入Pager对象中
        pager.setList(studentList);
        HttpSession session=request.getSession();
        session.setAttribute("begin",begin);
        session.setAttribute("end",end);
        session.setAttribute("pager",pager);
        session.setAttribute("currentPage",currentPage);
        session.setAttribute("currentSize",currentSize);
        session.setAttribute("totalPage",totalPage);
        session.setAttribute("stunum",stunum);
        session.setAttribute("name",name);
        session.setAttribute("specialty",specialty);


        return request.getContextPath()+"/adm-student.jsp";

    }

    //增加：
    public String add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 从请求参数中获取教师的各个属性值
        String stunum = request.getParameter("stunum");
        String name = request.getParameter("name");
        String gender = request.getParameter("gender");
        int age = 20;
        String agestr = request.getParameter("age");
        if(agestr!=null&&!"".equals(agestr)){
            age = Integer.parseInt(agestr);
        }
        String nation = request.getParameter("nation");
        String birthday = request.getParameter("birthday");
        String grade = request.getParameter("grade");
        String specialty = request.getParameter("specialty");
        String classes = request.getParameter("classes");
        String politicstatus = request.getParameter("politicstatus");
        String address = request.getParameter("address");
        String phone = request.getParameter("phone");

        //类型转换：
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date birthdaydate = null;
        if(birthday!=null&&!"".equals(birthday)){
            try {
                birthdaydate = simpleDateFormat.parse(birthday);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        // 创建Teacher对象并设置属性值
        Student student=new Student();
        student.setStunum(stunum);
        student.setName(name);
        student.setGender(gender);
        student.setAge(age);
        student.setNation(nation);
        student.setBirthday(birthdaydate);
        student.setGrade(grade);
        student.setSpecialty(specialty);
        student.setClasses(classes);
        student.setPoliticstatus(politicstatus);
        student.setAddress(address);
        student.setPhone(phone);
        // 创建TeacherDao对象
        StudentService studentService=new StudentServiceImpl();
        int flag = studentService.add(student);
        if (flag>0){
            System.out.println("插入成功");
            return request.getContextPath()+"/student?methodName=searchs";//插入成功 重定向学生列表
        }else {
            System.out.println("插入失败");
            return request.getContextPath()+"/adm-student.jsp";//插入失败
        }
    }

    //根据id查找信息 回显
    public void findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        String ids = request.getParameter("id");
        PrintWriter out = response.getWriter();
        StudentService studentService = new StudentServiceImpl();
        Student student = studentService.findById(ids);
        String json = JSON.toJSONString(student);
        out.print(json);
    }

    public void findById2(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        String ids = request.getParameter("id");
        PrintWriter out = response.getWriter();
        StudentService studentService = new StudentServiceImpl();
        Student student = studentService.findById(ids);
        out.print(student.getBirthday());
    }

    //修改选中教师信息
    public String modifyStudent(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        //取值
        int id = 1;
        String idstr = request.getParameter("id");
        if(idstr!=null&&!"".equals(idstr)){
            id = Integer.parseInt(idstr);
        }
        String stunum = request.getParameter("stunum");
        String name = request.getParameter("name");
        String gender = request.getParameter("gender");
        int age = 20;
        String agestr = request.getParameter("age");
        if(agestr!=null&&!"".equals(agestr)){
            age = Integer.parseInt(agestr);
        }
        String nation = request.getParameter("nation");
        String birthday = request.getParameter("birthday");
        String grade = request.getParameter("grade");
        String specialty = request.getParameter("specialty");
        String classes = request.getParameter("classes");
        String politicstatus = request.getParameter("politicstatus");
        String address = request.getParameter("address");
        String phone = request.getParameter("phone");
        //类型转换：
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date birthdate = null;
        try {
            birthdate = simpleDateFormat.parse(birthday);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        StudentService studentService = new StudentServiceImpl();
        //传值太多 创建一个学生类
        Student student = new Student();
        student.setId(id);
        student.setStunum(stunum);
        student.setName(name);
        student.setGender(gender);
        student.setAge(age);
        student.setNation(nation);
        student.setBirthday(birthdate);
        student.setGrade(grade);
        student.setSpecialty(specialty);
        student.setClasses(classes);
        student.setPoliticstatus(politicstatus);
        student.setAddress(address);
        student.setPhone(phone);
        int Count = studentService.modifyStudent(student);
        System.out.println(Count);

        return request.getContextPath()+"/student?methodName=searchs";
    }
}


